package org.javaforever.cookieshop.dao;

import org.javaforever.cookieshop.domain.GoodsBean;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import jakarta.transaction.Transactional;
public interface GoodsDao extends JpaRepository<GoodsBean, Long>{
	@Query("from GoodsBean g")
	public List<GoodsBean> listAllGoodses() throws Exception;

	@Modifying
	@Query("delete from GoodsBean g where g.id = :id")
	@Transactional
	public void deleteGoods(Long id) throws Exception;

	@Modifying
	@Query("update GoodsBean g set g.active = false where g.id = :id")
	@Transactional
	public void softDeleteGoods(Long id) throws Exception;

	@Query("from GoodsBean g where g.id = :id")
	public GoodsBean findGoodsById(Long id) throws Exception;

	@Query("from GoodsBean g where g.name = :name")
	public GoodsBean findGoodsByName(String name) throws Exception;

	@Query("from GoodsBean g where g.active = true")
	public List<GoodsBean> listActiveGoodses() throws Exception;

	@Modifying
	@Query("update GoodsBean g set g.active = not g.active where g.id = :id")
	@Transactional
	public void toggleGoods(Long id) throws Exception;

	@Modifying
	@Query("update GoodsBean g set g.active = true where g.id = :id")
	@Transactional
	public void activateGoods(Long id) throws Exception;

	@Query("from GoodsBean g where g.id = :id")
	public GoodsBean findGoodsFullInfoById(Long id) throws Exception;

	@Query("from GoodsBean g where g.name like CONCAT(:shortName, '%')")
	public List<GoodsBean> searchGoodsIndexedNamesByShortName(String shortName) throws Exception;

	@Query("select count(*) as countNum from GoodsBean g where g.active = true")
	public Integer countActiveGoodsRecords() throws Exception;
	
	@Query("select count(*) from GoodsBean g where g.name like CONCAT('%',:keyword, '%')")
	public int getSearchCount(String keyword);
	
	@Modifying
	@Query(value="insert into cs_recommends(name, active,type,goods) values('', true,:type,:id)", nativeQuery=true)
	@Transactional
	public void addRecommend(Long id,Integer type) throws Exception;
	
	@Modifying
	@Query(value="delete from cs_recommends where type=:type and goods=:id", nativeQuery=true)
	@Transactional
	public void removeRecommend(Long id,Integer type) throws Exception;
	
}
